#include "camera.h"


Camera::Camera(Vector position,
               Vector direction,
               double zoomFactor,
               double screenDistance,
               QObject *parent):
    QObject(parent),
    _position(position),
    _direction(direction),
    _zoomFactor(zoomFactor),
    _screenDistance(screenDistance),
    _coordinate(position, (Origine::worldUp() / (direction / zoomFactor)) / direction, direction, (Origine::worldUp() / (direction / zoomFactor)))
{

}

void Camera::moveFoward(double lenght)
{
    _position += _direction * lenght;
}

void Camera::moveBack(double lenght)
{
    _position -= _direction * lenght;
}

void Camera::moveRight(double lenght)
{
    _position += (Origine::worldUp() / _direction) * lenght;
}

void Camera::moveLeft(double lenght)
{
    _position -= (Origine::worldUp() / _direction) * lenght;
}

void Camera::moveUp(double lenght)
{
    _position += Origine::worldUp() * lenght;
}

void Camera::moveDown(double lenght)
{
    _position -= Origine::worldUp() * lenght;
}

void Camera::setZoomFactor(double factor)
{
    _zoomFactor = factor;
}

const Vector &Camera::position() const
{
    return _position;
}

const Vector &Camera::direction() const
{
    return _direction;
}

Ray Camera::getRay(double x, double y, double w, double h) const
{
    Vector add = _direction ;
    add += _coordinate.corner() *
            (w/h) *
            (0.5 - (x/w));
    add += _coordinate.up()     *
            (0.5 - (y/h));
    return Ray(_position + add * _screenDistance, Normalized(add));
}

QDataStream &operator<<(QDataStream & s, const Camera & camera)
{
    return s;
}

QDataStream &operator>>(QDataStream & s, Camera & camera)
{
    return s;
}
